package com.cskaoyan.demo2.servlet1;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;


@WebServlet("/admin/goods/*")
public class AdminGoodsServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uri = req.getRequestURI();
        String methodName = uri.substring(uri.lastIndexOf("/") + 1);
/*        //下面代码可以用反射来写
        if ("list".equals(methodName)) {
            list();
        } else if ("delete".equals(methodName)) {
            delete();

        }*/

        //改造为反射
        Class<AdminGoodsServlet> adminGoodsServletClass = AdminGoodsServlet.class;
        try {
            Method method = adminGoodsServletClass.getDeclaredMethod(methodName);
            Object methodStart = method.invoke(new AdminGoodsServlet());
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }

    }

    private void list() {
        System.out.println("i am list");
    }

    private void delete() {
        System.out.println("i am delete");
    }
}
